package com.xiaoyuan.minicore.aop;

import java.lang.reflect.Method;

/**
 * @Auther: xiaoyuan
 * @Date: 2020/4/30
 * @Description:
 */
public abstract class DefaultHandler implements Handler{

    @Override
    public Object proceed(Chain chain) throws Throwable {
        Method methodTarget = chain.getMethodTarget();
        Object[] args = chain.getArgs();
        Object result = null;
        if (filter(methodTarget, args)) {
            try {
                before(methodTarget, args);
                result = chain.proceed();
                after(methodTarget, args);
            } catch (Exception e) {
                error(methodTarget, args, e);
                throw new RuntimeException(e);
            } finally {
                end(methodTarget, args);
            }
        } else {
            result = chain.proceed();
        }
        return result;
    }

    @Override
    public boolean filter(Method method, Object[] args) {
        return true;
    }

    @Override
    public void end(Method method, Object[] args) {
    }

    @Override
    public void before(Method method, Object[] args) {
    }

    @Override
    public void after(Method method, Object[] args) {
    }

    @Override
    public void error(Method method, Object[] args, Exception e) {
    }


}
